home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / Macintosh Tracker 1.1 Source / Original Tracker 3.10 Source / audio.c < prev    next >
Text File  |  1993-05-22  |  7KB  |  278 lines

  1. /* audio.c */
  2.  
  3. /* $Id: audio.c,v 3.6 1992/11/27 10:29:00 espie Exp espie $
  4.  * $Log: audio.c,v $
  5.  * Revision 3.6  1992/11/27  10:29:00  espie
  6.  * General cleanup
  7.  *
  8.  * Revision 3.5  1992/11/24  10:51:19  espie
  9.  * Optimized output and fixed up some details.
  10.  * Unrolled code for oversample = 1 to be more efficient at
  11.  * higher frequency (since a high frequency is better than
  12.  * a higher oversample).
  13.  *
  14.  * Revision 3.4  1992/11/23  17:18:59  espie
  15.  * *** empty log message ***
  16.  *
  17.  * Revision 3.3  1992/11/23  10:12:23  espie
  18.  * Fixed up BIG bug.
  19.  *
  20.  * Revision 3.2  1992/11/20  14:53:32  espie
  21.  * Added finetune.
  22.  *
  23.  * Revision 3.1  1992/11/19  20:44:47  espie
  24.  * Protracker commands.
  25.  *
  26.  * Revision 3.0  1992/11/18  16:08:05  espie
  27.  * New release.
  28.  *
  29.  * Revision 2.14  1992/11/06  19:31:53  espie
  30.  * Fixed missing parameter type.
  31.  * fix_xxx for better speed.
  32.  * set_volume.
  33.  * Added possibility to get back to MONO for the sgi.
  34.  * Added stereo capabilities to the indigo version.
  35.  * Minor bug: a SAMPLE_FAULT is a minor error,
  36.  * we should first check that there was no other
  37.  * error before setting it.
  38.  * New resample function coming from the player.
  39.  * Added more notes.
  40.  *
  41.  * Revision 2.1  1991/11/17  23:07:58  espie
  42.  * Just computes some frequency-related parameters.
  43.  *
  44.  *
  45.  */
  46.  
  47. #include <math.h>
  48. #include <malloc.h>
  49. #include <stdio.h>
  50.  
  51. #include "defs.h"
  52. #include "extern.h"
  53. #include "song.h"
  54. #include "channel.h"
  55.      
  56. LOCAL char *id = "$Id: audio.c,v 3.6 1992/11/27 10:29:00 espie Exp espie $";
  57.  
  58. /* Have to get some leeway for vibrato (since we don't bound pitch with
  59.  * vibrato). This is conservative.
  60.  */
  61. #define VIB_MAXDEPTH 150
  62.  
  63.  
  64. #define C fix_to_int(ch->pointer)
  65.  
  66. LOCAL int step_table[MAX_PITCH + VIB_MAXDEPTH];  
  67.                     /* holds the increment for finding the next sampled
  68.                      * byte at a given pitch (see resample() ).
  69.                      */
  70.  
  71. /* creates a table for converting ``amiga'' pitch
  72.  * to a step rate at a given resampling frequency.
  73.  * For accuracy, we don't use floating point, but
  74.  * instead fixed point ( << ACCURACY).
  75.  * IMPORTANT NOTE: we need to make it fit within 32 bits, which
  76.  * must be enough for ACCURACY + log2(max sample length)
  77.  */
  78. LOCAL void create_step_table(oversample, output_fr)
  79. int oversample;     /* we sample oversample i for each byte output */
  80. int output_fr;      /* output frequency */
  81.     {
  82.     double note_fr; /* note frequency (in Hz) */
  83.     double step;
  84.     int pitch;      /* amiga pitch */
  85.  
  86.     step_table[0] = 0;
  87.     for (pitch = 1; pitch < MAX_PITCH + VIB_MAXDEPTH; pitch++)
  88.         {
  89.         note_fr = AMIGA_CLOCKFREQ / pitch;
  90.             /* int_to_fix(1) is the normalizing factor */
  91.         step = note_fr / output_fr * int_to_fix(1) / oversample;
  92.         step_table[pitch] = (int)step;
  93.         }
  94.     }
  95.          
  96. LOCAL void readjust_pitch(ch)
  97. struct channel *ch;
  98.     {
  99.     int i;
  100.     for (i = 0; i < NUMBER_TRACKS; i++)
  101.         ch[i].step = step_table[ch[i].cpitch];
  102.     }
  103.  
  104. void init_tables(oversample, frequency, chan)
  105. int oversample, frequency;
  106. struct channel *chan;
  107.     {
  108.     create_step_table(oversample, frequency);
  109.     if (chan)
  110.         readjust_pitch(chan);
  111.     }
  112.  
  113.  
  114. /* The playing mechanism itself.
  115.  * According to the current channel automata,
  116.  * we resample the instruments in real time to
  117.  * generate output.
  118.  */
  119. void resample(chan, oversample, number)
  120. struct channel *chan;
  121. int oversample;
  122. int number;
  123.     {
  124.     int i;          /* sample counter */
  125.     int channel;    /* channel counter */
  126.     int sampling;   /* oversample counter */
  127.     SAMPLE sample;  /* sample from the channel */
  128.     int byte[NUMBER_TRACKS];
  129.                     /* recombinations of the various data */
  130.     struct channel *ch;
  131.  
  132.         /* there used to be pointer tests around here, that
  133.          * are no longer needed 
  134.          */
  135.     /* special case oversampling for speedier stuff */
  136.     if (oversample == 1)
  137.         {
  138.             /* do the resampling, i.e., actually play sounds */
  139.         for (i = 0; i < number; i++) 
  140.             {
  141.             for (channel = 0; channel < NUMBER_TRACKS; channel++)
  142.                 {
  143.                 ch = chan + channel;
  144.                 switch(ch->mode)
  145.                     {
  146.                 case DO_NOTHING:
  147.                     byte[channel] = 0;
  148.                     break;
  149.                 case PLAY:
  150.                         /* Since we now have fix_length, we can
  151.                          * do that check with improved performance
  152.                          */
  153.                     if (ch->pointer < ch->samp->fix_length)
  154.                         {
  155.                         byte[channel] = ch->samp->start[C] * ch->volume;
  156.                         ch->pointer += ch->step;
  157.                         break;
  158.                         }
  159.                     else
  160.                         {
  161.                         ch->mode = REPLAY;
  162.                         ch->pointer -= ch->samp->fix_length;
  163.                         }
  164.                 case REPLAY:
  165.                             /* is there a replay ? */
  166.                     if (!ch->samp->rp_start)
  167.                         {
  168.                         ch->mode = DO_NOTHING;
  169.                         break;
  170.                         }
  171.                     while (ch->pointer >= ch->samp->fix_rp_length)
  172.                         ch->pointer -= ch->samp->fix_rp_length;
  173.                     byte[channel] = ch->samp->rp_start[C] * ch->volume;
  174.                     ch->pointer += ch->step;
  175.                     break;
  176.                     }
  177.                 } 
  178.             output_samples(byte[0]+byte[3], byte[1]+byte[2]);
  179.             }
  180.         }   
  181.     else
  182.         {
  183.             /* do the resampling, i.e., actually play sounds */
  184.         for (i = 0; i < number; i++) 
  185.             {
  186.             for (channel = 0; channel < NUMBER_TRACKS; channel++)
  187.                 {
  188.                 byte[channel] = 0;
  189.                 for (sampling = 0; sampling < oversample; sampling++)
  190.                     {
  191.                     ch = chan + channel;
  192.                     switch(ch->mode)
  193.                         {
  194.                     case DO_NOTHING:
  195.                         break;
  196.                     case PLAY:
  197.                             /* Since we now have fix_length, we can
  198.                              * do that check with improved performance
  199.                              */
  200.                         if (ch->pointer < ch->samp->fix_length)
  201.                             {
  202.                             sample = ch->samp->start[C];
  203.                             byte[channel] += sample * ch->volume;
  204.                             ch->pointer += ch->step;
  205.                             break;
  206.                             }
  207.                         else
  208.                             {
  209.                             ch->mode = REPLAY;
  210.                             ch->pointer -= ch->samp->fix_length;
  211.                             }
  212.                     case REPLAY:
  213.                                 /* is there a replay ? */
  214.                         if (!ch->samp->rp_start)
  215.                             {
  216.                             ch->mode = DO_NOTHING;
  217.                             break;
  218.                             }
  219.                         while (ch->pointer >= ch->samp->fix_rp_length)
  220.                             ch->pointer -= ch->samp->fix_rp_length;
  221.                         sample = ch->samp->rp_start[C];
  222.                         byte[channel] += sample * ch->volume;
  223.                         ch->pointer += ch->step;
  224.                         break;
  225.                         }
  226.                     } 
  227.                 }
  228.             output_samples((byte[0]+byte[3])/oversample, 
  229.                 (byte[1]+byte[2])/oversample);
  230.             }   
  231.         }
  232.  
  233.  
  234.     flush_buffer();
  235.     }
  236.  
  237.  
  238. /* setting up a given note */
  239.  
  240. void reset_note(ch, note, pitch)
  241. struct channel *ch;
  242. int note;
  243. int pitch;
  244.     {
  245.     ch->pointer = 0;
  246.     ch->mode = PLAY;
  247.     ch->pitch = pitch;
  248.     ch->note = note;
  249.     ch->viboffset = 0;
  250.     set_current_pitch(ch, pitch);
  251.     }
  252.  
  253. /* changing the current pitch (value
  254.  * may be temporary, and not stored
  255.  * in channel pitch, for instance vibratos.
  256.  */
  257. void set_current_pitch(ch, pitch)
  258. struct channel *ch;
  259. int pitch;
  260.     {
  261.         /* save current pitch in case we want to change
  262.          * the step table on the run
  263.          */
  264.     ch->cpitch = pitch;
  265.     ch->step = step_table[pitch];
  266.     }
  267.  
  268. /* changing the current volume. You HAVE to get through
  269.  * there so that it will work on EVERY machine.
  270.  */
  271. void set_current_volume(ch, volume)
  272. struct channel *ch;
  273. int volume;
  274.     {
  275.     ch->volume = MAX(MIN(volume, MAX_VOLUME), MIN_VOLUME);
  276.     }
  277.  
  278.